package example.util;

import example.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SysInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = Logger.getLogger(SysInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.debug("进入自定义拦截器 ==》 SysInterceptor");
        HttpSession session = request.getSession();
        //获取登录用户
        SysUser sysUser = (SysUser) session.getAttribute("USER_SESSION");
        //验证登录状态
        if (null == sysUser) {
            response.sendRedirect(request.getContextPath() + "/401.jsp");
            return false;
        }
        return true;
    }
}
